package com.jcodecraeer.core.net;
import android.content.Context;

import com.jcodecraeer.core.net.converter.scalars.ScalarsConverterFactory;
import com.jcodecraeer.core.net.cookie.CookieJarImpl;
import com.jcodecraeer.core.net.cookie.PersistentCookieStore;
import com.jcodecraeer.core.net.interceptor.HttpLoggingInterceptor;
import com.jcodecraeer.core.net.utils.OkLogger;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
public class RetrofitManager {
    private Retrofit mRetrofit;
    private volatile static RetrofitManager instance;
    private Context mContext;
    private boolean mDeBug = false;

    private RetrofitManager(Context context, boolean isPrintException) {
        this.mContext = context;
        this.mDeBug = isPrintException;
        //初始化拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkHttp");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        loggingInterceptor.setColorLevel(Level.INFO);
        OkLogger.debug(isPrintException);
        Interceptor contentInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
                        .addHeader("Content-Type", "text/html; harset=GB2312")
                        .addHeader("lang", "zh-cn")
                        .addHeader("Connection", "keep-alive")
                        .addHeader("Accept", "*/*")
                        .build();
                return chain.proceed(request);
            }
        };
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(7676, TimeUnit.MILLISECONDS)
                .connectTimeout(7676, TimeUnit.MILLISECONDS)
                .addInterceptor(loggingInterceptor)//拦截器，用于查看请求日志
                .addInterceptor(contentInterceptor)//拦截器，用于设置Content-Type
                .cookieJar(new CookieJarImpl(new PersistentCookieStore(context)))//设置Cookie持久化
                .build();
        //构建Retrofit对象
        mRetrofit = new Retrofit.Builder()
                .baseUrl("http://www.jcodecraeer.com/")
                .addConverterFactory(ScalarsConverterFactory.create())//设置数据解析工程
               .client(okHttpClient)//设置OkHttp客户端
                .build();
    }
    public static RetrofitManager getInstance(Context context, boolean debug) {
        if (instance == null) {
            synchronized (RetrofitManager.class) {
                if (instance == null) {
                    instance = new RetrofitManager(context, debug);
                }
            }
        }
        return instance;
    }

    public Retrofit getRetrofit() {
        if (mRetrofit == null) {
            mRetrofit = RetrofitManager.getInstance(mContext, mDeBug).initRetrofit();
        }
        return mRetrofit;
    }

    private Retrofit initRetrofit() {
        return mRetrofit;
    }
}